package com.dingqb.ktdemo.oop.delegatemode

import kotlin.properties.Delegates
import java.io.File

class User {
	var name: String by Delegates.observable("初始值") { prop, old, new ->
		println("旧值：$old -> 新值：$new")

	}

	var name2: String by Delegates.observable("初始值", { property, oldValue, newValue ->
		println("旧值：$oldValue -> 新值：$newValue")
	})
}

fun main(args: Array<String>) {
	val user = User()
	user.name = "第一次赋值"
	user.name = "第二次赋值"
	initSiteValue();

	initMap()
	initMutableMap();
	initList()
}

class Site(var map: Map<String, Any?>) {
	val name: String by map
	val url: String by map
}

fun initSiteValue() {
	var map = mapOf(
		"name" to "this is name",
		"url" to "this is url"
	);
	var site = Site(map);
	println("site name :" + site.name)
	println("site url :" + site.url)
}

fun initMap(): Map<*, *> {
	var map: Map<String, String>;
	map = mapOf("k1" to "kvalue", "k2" to "kvalue");
	println(map["k1"]);
	val map2 = mapOf("k3" to "k3value");
	return map;
}


fun initMutableMap(): MutableMap<*, *> {
	var mutable: MutableMap<*, *>;
	mutable = mutableMapOf("k1" to "mutableMap1", "k2" to "mutableMap2");
	println(mutable["k1"])
	mutable.forEach { key, value -> println("key==$key,value==$value") }
	mutable.forEach() { key, value -> println("key==$key,value==$value") }
	return mutable;
}

fun initList() {
	val l = listOf(11, 22, 33);
	for (i in 0..l.size - 1) {
		println(l[i]);
	}
	for (i in l) {
		println(i)
	}

}